package com.liwen.web.config;

import com.liwen.web.filter.LiwenFilter;
import com.liwen.web.interceptor.MyInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;


@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    //过滤器
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        List<String> urlPatterns = new ArrayList<String>();

        LiwenFilter testFilter = new LiwenFilter();   //new过滤器
        urlPatterns.add("/*");      //指定需要过滤的url
        filterRegistrationBean.setFilter(testFilter);
        filterRegistrationBean.setUrlPatterns(urlPatterns);
        return filterRegistrationBean;
    }

    //增加拦截器
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(new MyInterceptor())    //指定拦截器类
                .addPathPatterns("/*");        //指定该类拦截的url
    }
}
